﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Configuration;
using TrainingsApp.Logic;
using TrainingsApp.Model;
using TrainingsApp.WebModel.Route;
using TrainingsApp.WebModel.Debug;
using System.IO;
using TrainingsApp.WebModel;

/// <summary>
///PageBase 的摘要说明
/// </summary>
public abstract class PageBase : Page {
    public PageBase() {
        //this.Error += new EventHandler(PageBase_Error);
    }

    public RouteManager RouteHelper;


    protected override void Render(HtmlTextWriter writer) {
        if (writer is System.Web.UI.Html32TextWriter) {
            writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
        }
        else {
            writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
        }
        base.Render(writer);
    }

    public IEnumerable<T> GetAllPager<T>(int index, int size, List<T> list, int total) {
        int skipCount = index * size;

        List<T> temp = new List<T>(total);
        while (skipCount > 0) {
            temp.Add(default(T));
            skipCount--;
        }
        temp.AddRange(list);

        while (temp.Count < total) {
            temp.Add(default(T));
        }
        return temp;
    }

    public virtual void RedirectTo(string url) {
        Response.Redirect(url);
    }

    public virtual void RedirectTo(string url, string message) {
        MessageBox.Redirect(message, url);
    }

    protected override void OnPreInit(EventArgs e) {
        base.OnPreInit(e);
    }

    protected override void OnInit(EventArgs e) {

        RouteHelper = RouteManager.GetInstance(PassPort.UserStateProvider);
        return;
        UserContext user = CurrentUser;

        if ((user.FunctionRights == null || user.FunctionRights.Count == 0) && IsAuthenticated) {
            //跳转到无使用权限页面
            Response.Redirect("~/MLogin.aspx", true);
        }

        if (!user.FunctionRights.Where(a => !string.Equals(a.LINKURL, null)).ToList().Exists(a => a.LINKURL.ToUpper().Contains(Request.FilePath.ToUpper())) && IsAuthenticated) {
            //跳转到无使用权限页面
            Response.Redirect("~/Error.aspx", true);
        }

        base.OnInit(e);
    }

    protected void PageBase_Error(object sender, EventArgs e) {
        Exception ex = Server.GetLastError();
        ProcessUnhandledException(ex);
    }

    protected virtual void ProcessUnhandledException(string message) {
        Exception ex = new Exception(message);
        ProcessUnhandledException(ex);
    }

    protected virtual void ProcessUnhandledException(Exception ex) {
        if (ex == null)
            return;
        Response.Redirect("~/Error.aspx");
    }

    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        if (!IsPostBack) {
            UrlReferrer = Request.UrlReferrer == null ? "" : Request.UrlReferrer.ToString();
        }
    }

    protected virtual string UrlReferrer {
        get {
            object url = ViewState["UrlReferrer"];
            return url == null ? "" : ViewState["UrlReferrer"].ToString();
        }
        set {
            ViewState["UrlReferrer"] = value;
        }
    }

    public virtual bool IsAuthenticated {
        get {
            return true;
        }
    }

    public string LeftTreeStyle {
        get {

            return TrainingsApp.WebModel.Storage.UserCookieFacade.GetLeftTreeState() == 1 ? "" : "display:none;";
        }
    }

    public UserContext CurrentUser {
        get {

            UserContext user = PassPort.CurrentUser;
            //UserContext user = null;

            //if (user == null)
            //{
            //    user = TrainingsApp.Logic.Sys_Permission_Logic.GetUserInfo("gghha");
            //    Session["CurContext"] = user;
            //}


            if (user == null && IsAuthenticated) {
                Response.Redirect("~/UserLogout.ashx", true);
            }

            return user;
        }
    }

    public bool IsLogined { get { return this.CurrentUser != null && this.CurrentUser.UserType != UserType.Guest; } }


    internal class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter {
        private string _url; // 伪装的URL
        internal FormFixerHtml32TextWriter(TextWriter writer)
            : base(writer) {
            _url = HttpContext.Current.Request.Path + HttpContext.Current.Request.Url.Query;
        }
        public override void WriteAttribute(string name, string value, bool encode) {
            if (_url != null && string.Compare(name, "action", true) == 0) {
                value = _url;
            }
            base.WriteAttribute(name, value, encode);
        }
    }

    internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter {
        private string _url;
        internal FormFixerHtmlTextWriter(TextWriter writer)
            : base(writer) {
            _url = HttpContext.Current.Request.Path + HttpContext.Current.Request.Url.Query;
        }
        public override void WriteAttribute(string name, string value, bool encode) {
            if (_url != null && string.Compare(name, "action", true) == 0) {
                value = _url;
            }
            base.WriteAttribute(name, value, encode);
        }
    }

}
